<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\AdminUserModel;
use App\Models\AllocationApplicationModel;
use App\Models\CommonModel;
use App\Models\ContractModel;
use App\Models\LabourServicesExamineRecordModel;
use App\Models\LabourServicesModel;
use App\Models\MachineModel;
use App\Models\MaterialRequisitionModel;
use App\Models\MessageNotificationModel;
use App\Models\PaymentModel;
use App\Models\ProjectModel;
use App\Models\ProjectSubcontractModel;
use App\Models\PurchaseApplicationModel;
use App\Models\PurchaseOrderModel;
use App\Models\RepairApplicationModel;
use App\Models\RepairOrderModel;
use App\Models\SettlementModel;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    //消息首页
    public function index()
    {
        $review_last_type1 = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where(['type'=>1])->latest()->first();
        $review_last_type = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->whereIn('type',[2,3])->latest()->first();
        $examine_last_type1 = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where(['type'=>4])->latest()->first();
        $examine_last_type = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->whereIn('type',[5,6])->latest()->first();
        $data = [
            [
                'type' => '1',
                'type_name' => '待审阅',
                'num' => MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('is_handled', 0)->where(['type'=>1])->count(),
                'date' => $review_last_type1 ? $review_last_type1->created_at->format('Y/m/d') : '',
                'content' => $review_last_type1 ? $review_last_type1->detail : ''
            ],
            [
                'type' => '2',
                'type_name' => '审阅通知',
                'num' => MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('is_handled', 0)->whereIn('type',[2,3])->count(),
                'date' => $review_last_type ? $review_last_type->created_at->format('Y/m/d') : '',
                'content' => $review_last_type ? $review_last_type->detail : ''
            ],
            [
                'type' => '3',
                'type_name' => '待审核',
                'num' => MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('is_handled', 0)->where(['type'=>4])->count(),
                'date' => $examine_last_type1 ? $examine_last_type1->created_at->format('Y/m/d') : '',
                'content' => $examine_last_type1 ? $examine_last_type1->detail : ''
            ],
            [
                'type' => '4',
                'type_name' => '审核通知',
                'num' => MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('is_handled', 0)->whereIn('type',[5,6])->count(),
                'date' => $examine_last_type ? $examine_last_type->created_at->format('Y/m/d') : '',
                'content' => $examine_last_type ? $examine_last_type->detail : ''
            ],
        ];
        return response()->json(['code'=>'200','msg' => '提交成功','data'=>$data]);
    }

    //消息列表
    public function list($type, Request $request)
    {
        $page = $request->get('page', 1);
        $take = $request->get('take', 20);
        $seacher_title = $request->post('seacher_title');

        $data = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->when($type,function($query,$type){
            if($type == 1){
                return  $query->where(['type'=>1]);
            }elseif($type == 2){
                return  $query->whereIn('type',[2,3]);
            }elseif($type == 3){
                return  $query->where(['type'=>4]);
            }elseif($type == 4){
                return  $query->whereIn('type',[5,6]);
            }
        })->when($seacher_title,function($query,$seacher_title){
            return  $query->orWhere('code','like','%'.$seacher_title.'%');
        })->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        if($type == 2){
            MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->whereIn('type',[2,3])->where(['is_handled'=>0])->update(['is_handled'=>1]);
        }elseif($type == 4){
            MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->whereIn('type',[5,6])->where(['is_handled'=>0])->update(['is_handled'=>1]);
        }

        foreach($data as $key=>$val){
            $staff = AdminUserModel::where('id',$val['admin_id'])->select(['name','company_id','company_name','department_id','department_name'])->first();
            $application_detail = CommonModel::ApplicationList($val['route_type'],$val['code']);
            $data[$key]['staff'] = $staff;
            $data[$key]['remark'] = $application_detail['remark'];
            $data[$key]['status'] = $application_detail['status'];
        }
        return response()->json(['code'=>'200','msg' => '提交成功','data'=>$data]);

    }

    //消息列表
    public function my_approval(Request $request)
    {
        $page = $request->post('page', 1);
        $take = $request->post('take', 10);
        $type = $request->post('type', 1);

        $seacher_title = $request->post('seacher_title');

        $order_type = $request->post('order_type');

        $start_time = $request->post('start_time');

        $end_time = $request->post('end_time');

        $data = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->when($type,function($query,$type){
            if($type == 1){
                return  $query->whereIn('type',[4])->where(['is_handled'=>0]);
            }elseif($type == 2){
                return  $query->whereIn('type',[4])->where(['is_handled'=>1]);
            }elseif($type == 3){
                return  $query->whereIn('type',[1]);
            }
        })->when($seacher_title,function($query,$seacher_title){
            return  $query->orWhere('code','like','%'.$seacher_title.'%');
        })->when($order_type,function($query,$order_type){
            return  $query->where('route_type',$order_type);
        })->when($start_time,function($query,$start_time){
            return  $query->where('created_at','>=',date('Y-m-d H:i:s',$start_time));
        })->when($end_time,function($query,$end_time){
            return  $query->where('created_at','=<',date('Y-m-d H:i:s',$end_time));
        })->orderBy('created_at','desc')->skip(($page - 1) * $take)->take($take)->get();

        foreach($data as $key=>$val){

            $staff = AdminUserModel::where('id',$val['admin_id'])->select(['name','company_id','company_name','department_id','department_name'])->first();
            $application_detail = CommonModel::ApplicationList($val['route_type'],$val['code']);
            $data[$key]['staff'] = $staff;
            $data[$key]['remark'] = $application_detail['remark'];
            $data[$key]['status'] = $application_detail['status'];
        }
        $list['list'] = $data;
        $list['count']['examine_count'] = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('type',4)->where(['is_handled'=>0])->count();
        $list['count']['review_count'] = MessageNotificationModel::where('notifier_id', auth('api')->user()->id)->where('type',1)->where(['is_handled'=>0])->count();

        return response()->json(['code'=>'200','msg' => '提交成功','data'=>$list]);

    }

    //TODO:处理消息
    public function handle(Request $request)
    {

        $member_id = auth('api')->user()->id;

        $message_id = $request->post('message_id', 1);

        $handle = $request->post('handle', 1);

        $remark = $request->post('remark', 1);

        $message_list = MessageNotificationModel::where(['id'=>$message_id])->first();

        MessageNotificationModel::where(['id'=>$message_id])->update([
            'is_handled'=>1
        ]);

        if(empty($message_list)){
            return response()->json(['code'=>'200','msg' => '已处理']);
        }

        if($message_list['route_url'] == 1){
            if($message_list['type'] == 1){
                $data = PurchaseApplicationModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = PurchaseApplicationModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 2){
            if($message_list['type'] == 1){
                $data = PurchaseOrderModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = PurchaseOrderModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 3){
            if($message_list['type'] == 1){
                $data = AllocationApplicationModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = AllocationApplicationModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 5){
            if($message_list['type'] == 1){
                $data = MaterialRequisitionModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = MaterialRequisitionModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 6){
            if($message_list['type'] == 1){
                $data = RepairApplicationModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = RepairApplicationModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 7){
            if($message_list['type'] == 1){
                $data = RepairOrderModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = RepairOrderModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 8){
            if($message_list['type'] == 1){
                $data = ContractModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = ContractModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 9){
            if($message_list['type'] == 1){
                $data = PaymentModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = PaymentModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 10){
            if($message_list['type'] == 1){
                $data = SettlementModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = SettlementModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 11){
            if($message_list['type'] == 1){
                $data = ProjectModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = ProjectModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 12){
            if($message_list['type'] == 1){
                $data = ProjectSubcontractModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = ProjectSubcontractModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 13){
            if($message_list['type'] == 1){
                $data = LabourServicesModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = LabourServicesModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }elseif($message_list['route_url'] == 14){
            if($message_list['type'] == 1){
                $data = MachineModel::ReviewStart($message_list['code'],$handle,$remark,$member_id);
            }elseif($message_list['type'] == 4){
                $data = MachineModel::ExamineStart($message_list['code'],$handle,$remark,$member_id);
            }
        }

        $list = json_decode($data,true);

        return response()->json(['code'=>$list['code'],'msg' => $list['msg']]);

    }
}
